import { createApp,computed } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import PageHead from './components/pagehead.vue'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'


router.beforeEach((to, from) => {
  const token = localStorage.getItem('pz_token');
  if (!token && to.path !== '/login') {
    return '/login';
  } else if(token && to.path === '/login'){
    return '/';
  } else{
    return true;
  }
})

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
const app = createApp(App);
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.component(PageHead)
app.use(pinia)
import { useMenuStore } from "./store/index";
const useMenuData = useMenuStore();
let localStorageData = localStorage.getItem('menu');
if (localStorageData) {
  useMenuData.ChangRouter(JSON.parse(localStorageData).routeList)
  useMenuData.routeList.forEach((item) => {
    router.addRoute("main", item);
  });
}
app.use(router)
app.mount('#app')



